﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class VWVKP : ViewBase
    {
        #region ==SQTYEND===========
        public static readonly DependencyProperty SQTYENDProperty = DependencyProperty.Register("SQTYEND",
            typeof(double), typeof(VWVKP),
            new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSQTYENDChanged));
        public double SQTYEND
        {
            get
            {
                return (double)GetValue(SQTYENDProperty);
            }
            set
            {
                SetValue(SQTYENDProperty, value);
            }
        }
        private static void OnSQTYENDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as VWVKP;
            if (src != null)
            {
                src.SQTYEND = (double)e.NewValue;
            } 
        }
        #endregion
        #region ==REMARK===========
        public static readonly DependencyProperty REMARKProperty = DependencyProperty.Register("REMARK",
            typeof(string), typeof(VWVKP),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnREMARKChanged));
        public string REMARK
        {
            get
            {
                return (string)GetValue(REMARKProperty);
            }
            set
            {
                SetValue(REMARKProperty, value);
            }
        }
        private static void OnREMARKChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as VWVKP;
            if (src != null)
            {
                src.REMARK = (string)e.NewValue;
            }
        }
        #endregion
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        string KRS
        {
            get
            {
                return vm.KRS;
            }
            set
            {
                vm.KRS = Helper.Right("   " + value, 2); ;
            }
        }
        string LFD
        {
            get
            {
                return vm.LFD;
            }
            set
            {
                vm.LFD = Helper.Right("000000" + value, 6); ;
            }
        }
        string POS
        {
            get
            {
                return vm.POS;
            }
            set
            {
                vm.POS = Helper.Right("000" + value, 3);
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public decimal LDAB05
        {
            get
            {
                return Convert.ToDecimal(txtLDAB05.Text.ToString());
            }
        }
        public decimal LDAB55
        {
            get
            {
                return Convert.ToDecimal(txtLDAB55.Text.ToString());
            }
        }
        public decimal LDAB45
        {
            get
            {
                return Convert.ToDecimal(txtLDAB45.Text.ToString());
            }
        }
        public decimal LDAB50
        {
            get
            {
                return Convert.ToDecimal(txtLDAB50.Text.ToString());
            }
        }
        public bool IsAB05
        {
            get
            {
                if (ChkAB05.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
        }
        public bool IsAB55
        {
            get
            {
                if (ChkAB55.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
        }
        public bool IsAB45
        {
            get
            {
                if (ChkAB45.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
        }
        public bool IsAB50
        {
            get
            {
                if (ChkAB50.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
        }
        public bool CanReplan
        {
            get { return vm.CanReplan; }
            set
            {
                vm.CanReplan = value; 
            } 
        } 
        bool IsLoaded = false;
        SETINTEXDB SetMaster
        {
            get
            {
                return vm.SetMaster;
            }
        }
        SETINTEXDB SetEnv
        {
            get
            {
                return vm.SetEnv;
            }
        }
        SETINTEXDB SetDetail
        {
            get
            {
                return vm.SetDetail;
            }
        }
        CollectionViewSource vwVKPOS
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwVKPOS");
            }
        }
        DataRow dwVKPOS
        {
            get
            {
                if (vwVKPOS.View == null)
                    return null;
                if ((DataRowView)vwVKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwVKPOS.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwBASTGA
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwBASTGA");
            }
        }
        DataRow dwBASTGA
        {
            get
            {
                if (vwBASTGA.View == null)
                    return null;
                if ((DataRowView)vwBASTGA.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwBASTGA.View.CurrentItem).Row;
            }
        }
        MainViewModel vm;
        string PlanID = "JLT01";
        public VWVKP()
        {

            InitializeComponent();
            this.Loaded += Windows_Loaded;
         
        }

        void Windows_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            IsLoaded = true;
            if (vm == null)
            {
                vm = this.DataContext as MainViewModel;
            } 
            vm.PlanID = PlanID;
            ChkAB50.IsChecked = true;
            vm.IsAB05 = IsAB05;
            vm.IsAB55 = IsAB55;
            vm.IsAB45 = IsAB45;
            vm.IsAB50 = IsAB50;
            vwVKPOS.View.CurrentChanged += vwVKPOS_CurrentChanged;
           
            if (app.User.Trim() == "JLTV")
            {

                KRS = "J5";
                LFD = "579";
                cmbLoomType.Text = "喷气";
                cmbWarpType.Text = "分批整经";
                btnQuery_Click(null, null);
            }
        }
        bool IsResetPamameter = true;
        void vwVKPOS_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                if (dwVKPOS == null)
                    return;
                REMARK = "";
                vm.CanPublish = false;
               
                vm.dwVKP = dwVKPOS;

                if (vm.FilterData())
                {

                }

                if (SetDetail.ICC_ABAX.Rows.Count > 0)
                {
                    RefreshUI();
                    CanReplan = false;
                    vm.CanDelete = true;
                }
                else
                {
                    CanReplan = true;
                    vm.CanDelete = false;
                }
                CheckCAD();
                if (!CanReplan)
                    return;
                RefreshUI();
                //if (IsResetPamameter)
                {
                    ResetPamameter();
                }
                IsResetPamameter = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataSet SResult = new DataSet();
                DataRow dwData = SetMaster.VKPOS.NewRow();
                dwData["VKPMANDANT"] = app.Mandant;
                dwData["VKPAUFKRS"] = KRS;
                dwData["VKPAUFLFD"] = LFD;
                vm.LoadData(dwData);
                SetMaster.AcceptChanges();
                if (vwVKPOS.View != null)
                {
                    vwVKPOS.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnResetP_Click(object sender, RoutedEventArgs e)
        {
            ResetPamameter();
        }
        public void CheckCAD()
        {
            string filter;
            DataTable tb;
           
            filter = "WDAUFKRS='" + KRS + "' AND WDAUFLFD='" + LFD + "' AND WDAUFPOS='" + POS + "'";
            tb = SetDetail.Tables["WTGEW"].Select(filter).CopyToDataTable();
            foreach (DataRow dwWTGEW in tb.Rows)
            {
                vm.NRRWIDTH= Convert.ToDouble(dwWTGEW["WTGWBBRT"].ToString()) / 10;
                vm.WTGFDLGS =Helper.RoundX(2.54 * Convert.ToDouble(dwWTGEW["WTGFDLGS"].ToString()) / 10,1);
                if (CanReplan)
                {
                    vm.RRWIDTH = vm.NRRWIDTH;
                    vm.RWGross = vm.NRRWIDTH;
                    vm.NRWGross = vm.NRRWIDTH;
                }
                else
                {
                    vm.RRWIDTH = vm.NRRWIDTH;
                    vm.NRWGross = vm.NRRWIDTH;
                }
                break;
            }

            filter = "WDAUFKRS='" + KRS + "' AND WDAUFLFD='" + LFD + "' AND WDAUFPOS='" + POS + "'";
            tb = SetDetail.Tables["WASTAMM"].Select(filter).CopyToDataTable();
            foreach (DataRow dwWASTAMM in tb.Rows)
            {

                if (CanReplan)
                {
                    txtSR01.Text = (Convert.ToDouble(dwWASTAMM["WAALKV"].ToString())).ToString(); //投料系数 
                    txtFR02.Text = "0";// (Convert.ToDouble(dwWASTAMM["WAFGWTOL"].ToString()) / 100).ToString(); //零次布  
                    txtNSR01.Text = (Convert.ToDouble(dwWASTAMM["WAALKV"].ToString())).ToString(); //投料系数 
                    txtNFR02.Text = (Convert.ToDouble(dwWASTAMM["WAFGWTOL"].ToString()) / 100).ToString(); //零次布  
                }
                else
                {
                    txtNSR01.Text = (Convert.ToDouble(dwWASTAMM["WAALKV"].ToString())).ToString(); //投料系数
                    txtNFR01.Text = (Convert.ToDouble(dwWASTAMM["WALAEAEND"].ToString()) / 100).ToString();//整理缩率
                    txtNFR02.Text = (Convert.ToDouble(dwWASTAMM["WAFGWTOL"].ToString()) / 100).ToString(); //零次布  
                }
                break;
            } 
            foreach (DataRow dwWAF in SetDetail.Tables["WAFARBEN"].Rows)
            {

                if (CanReplan)
                {
                    vm.FR01 = Convert.ToDouble(dwWAF["WAFVGRQM"].ToString()) / 100;//整理缩率
                    txtNFR01.Text = (Convert.ToDouble(dwWAF["WAFVGRQM"].ToString()) / 100).ToString();//整理缩率 
                }
                else
                {
                    txtNFR01.Text = (Convert.ToDouble(dwWAF["WAFVGRQM"].ToString()) / 100).ToString();//整理缩率
                 }
                break;
            }
            filter = "WDAUFKRS='" + KRS + "' AND WDAUFLFD='" + LFD + "' AND WDAUFPOS='" + POS + "'";
            tb = SetDetail.Tables["WASTKE"].Select(filter).CopyToDataTable();
            DataRow dwCloth=null;
            foreach (DataRow dwWWASTKE in tb.Rows)
            {
                dwCloth = dwWWASTKE;

                if (CanReplan)
                {
                    txtRR01.Text = (100 * Convert.ToDouble(dwWWASTKE["WAKEAFAKTR"].ToString()) / 1000 - 100).ToString(); //织缩
                    txtNRR01.Text = (100 * Convert.ToDouble(dwWWASTKE["WAKEAFAKTR"].ToString()) / 1000 - 100).ToString(); //织缩
                }
                else
                {
                    txtNRR01.Text = (100 * Convert.ToDouble(dwWWASTKE["WAKEAFAKTR"].ToString()) / 1000 - 100).ToString(); //织缩
                }
                break;
            }
            if (dwCloth == null)
                return;
            string FClothNo = dwCloth["WAKART"].ToString().Trim() + "-" + dwCloth["WAKWCOL"].ToString().Trim();
            filter = "FClothNo='" + FClothNo + "'";
            vm.REMARK = "";
            DataRow[] rows = SetMaster.Tables["TechMaster"].Select(filter);
            foreach (DataRow row in rows)
            {
                vm.REMARK += "织造备注:" +"注1:"+ row["RREMARK1"].ToString().Trim() +"   "+"注2:"+ row["RREMARK2"].ToString().Trim() +"   " +"注3:"+row["RREMARK3"].ToString().Trim() + Environment.NewLine;
                vm.REMARK += "整经备注:" + row["WREMARK1"].ToString().Trim() + row["WREMARK2"].ToString().Trim() + row["WREMARK3"].ToString().Trim() + Environment.NewLine;
                vm.REMARK += "纬纱单备注:" + row["WFREMARK1"].ToString().Trim() + row["WFREMARK2"].ToString().Trim() + row["WFREMARK3"].ToString().Trim() + Environment.NewLine;
            }


        }
        private void ResetPamameter()
        {
            string VKPLIEFTERM = dwVKPOS["VKPLIEFTERM"].ToString();
            double VKPMENGE1 =0;
            string VKPMEEHFKT = dwVKPOS["VKPMEEHFKT"].ToString();
            var qData = from XD in SetMaster.VKPOS.AsEnumerable()
                        where XD["VKPART"].ToString()==dwVKPOS["VKPART"].ToString()
                        && XD["VKPFARBE"].ToString() == dwVKPOS["VKPFARBE"].ToString()
                        group XD by new
                        {
                            LO = XD["VKPART"].ToString(),
                            LB = XD["VKPFARBE"].ToString(),
                        }
                            into g
                            select new
                            {
                                KEY = g.Key,
                                VKPMENGE1 = g.Sum(XD => Convert.ToDouble(XD["VKPMENGE1"].ToString())),
                                STKS = g.Count()
                            };
            foreach (var xd in qData)
            {
                VKPMENGE1 = xd.VKPMENGE1; 
            } 
            SQTYEND = Math.Ceiling(Convert.ToDouble(VKPMENGE1) * Convert.ToDouble(VKPMEEHFKT) * Convert.ToDouble(0.001) * Convert.ToDouble(0.001) * Convert.ToDouble(0.01)); 
            txtDAB05.Text = VKPLIEFTERM;
            string OVERSHORT = dwVKPOS["OVERSHORT"].ToString();
            //int idxstart = OVERSHORT.IndexOfAny(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
            //int idxend = OVERSHORT.LastIndexOfAny(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
            //OVERSHORT = OVERSHORT.Substring(idxend, 1);
            txtSR01DES.Text = OVERSHORT;
            if (SetEnv.Tables.Contains("PRULES"))
                SetEnv.Tables["PRULES"].Clear();
            string filter = "PLANID='" + PlanID + "'";
            DataTable tb = SetEnv.Tables["ICC_PRULES"].Select(filter).CopyToDataTable();
            tb.TableName = "PRULES";
            SetEnv.Merge(tb);
            var qRules = from DataRow dwRule in SetEnv.Tables["PRULES"].AsEnumerable()
                         where dwRule["PLANID"].ToString().Trim() == PlanID
                         select dwRule;
            foreach (DataRow dwRule in qRules)
            {
                switch (dwRule["ARTSTAGE"].ToString().Trim())
                {
                    case "S":
                        break;
                    case "F":
                        //txtFR01.Text = dwRule["R01"].ToString();
                        //txtFR02.Text = dwRule["R02"].ToString();
                        //txtLDAB05.Text = dwRule["LEADTIME"].ToString();
                        txtFQTYEND.Text = "-1";
                        break;
                    case "R":
                        //txtRR01.Text = dwRule["R01"].ToString();
                        txtRJNUM.Text = "1";// dwRule["JNUM"].ToString();
                        txtRJWASTGE.Text =dwRule["JWASTGE"].ToString();
                        //txtLDAB55.Text = dwRule["LEADTIME"].ToString();
                        txtRQTYEND.Text = "-1";
                        //txtDAB55.Text = "0";
                        break;
                    case "W":
                        txtKR01.Text = dwRule["R01"].ToString();
                        txtKF01.Text = dwRule["F01"].ToString();
                        txtKJNUM.Text = dwRule["JNUM"].ToString();
                        txtKJWASTGE.Text = dwRule["JWASTGE"].ToString();                         
                        //txtLDAB45.Text = dwRule["LEADTIME"].ToString();
                        txtKQTYEND.Text = "-1";
                        txtWQTYEND.Text = "-1";
                        //txtDAB45.Text = "0";
                        break;

                    case "G":
                       vm.GR01 =Convert.ToDouble( dwRule["R01"].ToString());
                       vm.GR02 = Convert.ToDouble(dwRule["R02"].ToString());
                       vm.GF01 =Convert.ToDouble( dwRule["F01"].ToString());
                       vm.GF02= Convert.ToDouble(dwRule["F02"].ToString());    
                        //txtDAB50.Text = "0";
                       // txtLDAB50.Text = dwRule["LEADTIME"].ToString();
                        break;
                }
            }
        }

        private void txtVKPAUFLFD_KeyUp(object sender, KeyEventArgs e)
        { 
            

            if 
                (e.Key == System.Windows.Input.Key.Enter
                || e.Key == System.Windows.Input.Key.Return
                )
            {
                this.Focus();
                btnQuery_Click(null, null);
            }
        }

        private void btnPrePlan_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Focus();
                if (this.cmbLoomType.Text.ToString().Trim().Length < 2)
                {
                    MessageBox.Show("请选择织机类型");
                    return;
                }
                if (this.cmbWarpType.Text.ToString().Trim().Length < 2)
                {
                    MessageBox.Show("请选择整经方式");
                    return;
                }
                if (dwVKPOS == null)
                {
                    MessageBox.Show("请选择销售单行");
                    return;
                }
                if (txtDAB05.Text.ToString().Trim().Length < 8)
                {
                    MessageBox.Show("请设定交期");
                    return;
                }
                if (txtRJNUM.Text.Trim()=="0")
                {
                    MessageBox.Show("轴数至少为1");
                    return;
                }
               
                #region======准备用户参与数据=======
                DataSet SResult = null;
                bool result = false;
                var query = from DataRow row in SetEnv.Tables["PRULES"].AsEnumerable()
                            select row;
                foreach (DataRow dwRule in query)
                {
                    switch (dwRule["ARTSTAGE"].ToString())
                    {
                        case "S":
                            dwRule["R01"] = txtSR01.Text;
                            dwRule["R01DES"] = txtSR01DES.Text.Trim().PadRight(20).Substring(0,20);
                            dwRule["F01"] = txtSF01.Text;
                            dwRule["QTYEND"] = SQTYEND;
                            dwRule["DELDATE"] = txtDAB05.Text;
                            dwRule["REMARK"] = REMARK; 
                            break;
                        case "F":
                            dwRule["QTYEND"] = txtFQTYEND.Text;
                            dwRule["R01"] = vm.FR01 * 100;
                            dwRule["R02"] = txtFR02.Text;
                            dwRule["F02"] = dwVKPOS["PREALLOCATED"];
                            dwRule["DELDATE"] = txtDAB05.Text;
                            //dwRule["LEADTIME"] = txtLDAB05.Text;
                            dwRule["REMARK"] = REMARK; 
                            break;
                        case "R":
                            dwRule["QTYEND"] = txtRQTYEND.Text;
                            dwRule["R01"] = txtRR01.Text;
                            dwRule["JNUM"] = txtRJNUM.Text;
                            dwRule["JWASTGE"] = txtRJWASTGE.Text;
                           // dwRule["DELDATE"] = txtDAB55.Text;
                            //dwRule["LEADTIME"] = txtLDAB55.Text;
                            dwRule["F01"] =vm.WTGFDLGS;
                            dwRule["F02"] = vm.RWGross;
                            dwRule["R01DES"] = Helper.ConvertGB2312To8859P1(cmbLoomType.Text);
                            break;
                        case "W":
                            dwRule["QTYEND"] = txtKQTYEND.Text;
                            dwRule["R01"] = txtKR01.Text;
                            dwRule["F01"] = txtKF01.Text;
                            dwRule["JNUM"] = txtKJNUM.Text;
                            dwRule["JWASTGE"] = txtKJWASTGE.Text;
                           // dwRule["DELDATE"] = txtDAB45.Text;
                            //dwRule["LEADTIME"] = txtLDAB45.Text;
                            dwRule["R01DES"] = Helper.ConvertGB2312To8859P1(this.cmbWarpType.Text);
                            break;
                        case "K":
                            dwRule["QTYEND"] = txtWQTYEND.Text;
                            break;
                        case "G":
                            dwRule["QTYEND"] = "-1";
                            dwRule["R01"] = vm.GR01;
                            dwRule["R02"] = vm.GR02;
                            dwRule["F01"] = vm.GF01;
                            dwRule["F02"] = vm.GF02;
                            //dwRule["DELDATE"] = txtDAB50.Text;
                            //dwRule["LEADTIME"] = txtLDAB50.Text;
                            break;
                        case "B":
                            dwRule["QTYEND"] = "-1";
                            //dwRule["DELDATE"] = txtDAB50.Text;
                            //dwRule["LEADTIME"] = txtLDAB50.Text;
                            break;
                    }
                }

                #endregion
                SResult = vm.PrePlan(SetEnv, SetDetail, dwVKPOS);
                SetDetail.ABPOS.Clear();
                SetDetail.ICC_ABAX.Clear();
                SetDetail.Merge(SResult.Tables["ABPOS"]);
                SetDetail.Merge(SResult.Tables["ICC_ABAX"]);
                SetEnv.Tables["ICC_ABAX"].Clear();
                SetEnv.Merge(SetDetail.Tables["ICC_ABAX"]);
                RefreshUI();
                vm.CanPublish = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void RefreshUI()
        {
            DataRow dwABP = null;
            string filter = "";
            DataRow[] rows = null; 
            filter = "ABPAUFART='VA'";
            rows = SetDetail.Tables["ABPOS"].Select(filter);
            foreach (DataRow dwMU in SetDetail.Tables["ICC_ABAX"].Rows)
            {
                string ABPWAKZ = dwMU["ABPWAKZ"].ToString();
                string ABAWAKZ = dwMU["ABAWAKZ"].ToString();

                string ABAVKAUFTYP = dwMU["ABAVKAUFTYP"].ToString();
                string ABAVKAUFKRS = dwMU["ABAVKAUFKRS"].ToString();
                string ABAVKAUFPOS = dwMU["ABAVKAUFPOS"].ToString();
                string UNDES = dwMU["UNDES"].ToString();

                if (ABAVKAUFTYP == "K")
                {

                    //continue;
                }
                switch (ABAWAKZ)
                {
                    case "F":
                        //txtSQTYEND.Text = dwMU["QTYEND"].ToString();
                        txtSR01.Text = dwMU["R01"].ToString();
                        txtSR01DES.Text = dwMU["R01DES"].ToString();
                        txtDAB05.Text = dwMU["ABAVKSTARTD"].ToString();
                        txtFQTYEND.Text = (Convert.ToDouble(dwMU["ABARESMG"].ToString()) / 1000).ToString(); 
                        /*
                        MainViewModel.DAB05 = row["ABPLIEFDAT"].ToString();
                        MainViewModel.FQTYEND = (Convert.ToDouble(row["ABPSOLLMENGE2"].ToString()) / 100).ToString();
                        MainViewModel.FQTYMAT = (Convert.ToDouble(row["ABPSOLLMENGE1"].ToString()) / 100).ToString();
                        txtDAB05.Text = dwMU["DELDATE"].ToString();
                        txtFQTYEND.Text = (Convert.ToDouble(dwMU["ABPSOLLMENGE2"].ToString()) / 100).ToString();
                        txtFQTYMAT.Text = (Convert.ToDouble(dwMU["ABPSOLLMENGE1"].ToString()) / 100).ToString();*/
                        break;
                    case "R":
                         REMARK = dwMU["REMARK"].ToString();
                        vm.FR01 = Convert.ToDouble(dwMU["R01"].ToString()) / 100;
                        txtFR02.Text = dwMU["R02"].ToString();
                        txtLDAB05.Text = dwMU["LEADTIME"].ToString();
                        txtDAB55.Text = dwMU["ABAVKSTARTD"].ToString();
                        txtRQTYEND.Text = (Convert.ToDouble(dwMU["ABARESMG"].ToString()) / 1000).ToString();
                        break;
                    case "W":
                        txtRJNUM.Text = dwMU["JNUM"].ToString();
                        txtRJWASTGE.Text = dwMU["JWASTGE"].ToString();
                        txtLDAB55.Text = dwMU["LEADTIME"].ToString();
                        txtRR01.Text = dwMU["R01"].ToString();
                        vm.WTGFDLGS = Convert.ToDouble(dwMU["F01"].ToString());
                        vm.RRWIDTH = Convert.ToDouble(dwMU["F02"].ToString());       
                        vm.RWGross =Convert.ToDouble(dwMU["F02"].ToString());                        
                        cmbLoomType.Text = Helper.Convert8859P1ToGB2312(dwMU["R01DES"].ToString().Trim());

                        txtDAB45.Text = dwMU["ABAVKSTARTD"].ToString();
                        txtKQTYEND.Text = (Convert.ToDouble(dwMU["ABARESMG"].ToString()) / 1000).ToString();
                        /*
                        MainViewModel.DAB55 = row["ABPLIEFDAT"].ToString();
                        MainViewModel.RQTYEND = (Convert.ToDouble(row["ABPSOLLMENGE1"].ToString()) / 100).ToString();
                        MainViewModel.RQTYMAT = (Convert.ToDouble(row["ABPSOLLMENGE1"].ToString()) / 100).ToString();
                        txtDAB55.Text = dwMU["ABPLIEFDAT"].ToString();
                        txtRQTYEND.Text = (Convert.ToDouble(dwMU["ABPSOLLMENGE1"].ToString()) / 100).ToString();
                        txtRQTYMAT.Text = (Convert.ToDouble(dwMU["ABPSOLLMENGE1"].ToString()) / 100).ToString();
                         * */
                        break;
                    case "K":
                        /*
                        MainViewModel.DAB45 = row["ABPLIEFDAT"].ToString();

                        MainViewModel.KQTYEND = (Convert.ToDouble(row["ABPSOLLMENGE1"].ToString()) / 100).ToString();
                        MainViewModel.KQTYMAT = (Math.Ceiling(Convert.ToDouble(row["ABPSOLLMENGE5"].ToString()) / 100)).ToString();
                      
                        txtDAB45.Text = dwMU["ABPLIEFDAT"].ToString();
                        txtKQTYEND.Text = (Convert.ToDouble(dwMU["ABPSOLLMENGE1"].ToString()) / 100).ToString();
                        //txtWQTYEND.Text = (Convert.ToDouble(dwMU["ABPSOLLMENGE2"].ToString()) / 100).ToString();
                        txtKQTYMAT.Text = (Math.Ceiling(Convert.ToDouble(dwMU["ABPSOLLMENGE5"].ToString()) / 100)).ToString();
                         *   */
                        break;
                    case "G":
                        if (ABPWAKZ == "R")
                        {


                        }
                        if (ABPWAKZ == "W")
                        {
                            txtLDAB45.Text = dwMU["LEADTIME"].ToString();
                            txtKR01.Text = dwMU["R01"].ToString();
                            txtKF01.Text = dwMU["F01"].ToString();
                            txtKJNUM.Text = dwMU["JNUM"].ToString();
                            txtKJWASTGE.Text = dwMU["JWASTGE"].ToString();
                            cmbWarpType.Text = Helper.Convert8859P1ToGB2312(dwMU["R01DES"].ToString().Trim());
                            txtWQTYEND.Text = (Convert.ToDouble(dwMU["F02"].ToString()) / 100).ToString();
                        }
                        if (ABPWAKZ == "G")
                        {
                            vm.GR01 =Convert.ToDouble( dwMU["R01"].ToString());
                            vm.GR02 = Convert.ToDouble(dwMU["R02"].ToString());
                            vm.GF01 = Convert.ToDouble(dwMU["F01"].ToString());
                            vm.GF02 = Convert.ToDouble(dwMU["F02"].ToString());
                            txtLDAB50.Text = dwMU["LEADTIME"].ToString();
                            txtDAB50.Text = dwMU["ABAVKSTARTD"].ToString(); 
                        }
                        break;
                    default:
                        //MainViewModel.DAB50 = row["ABPLIEFDAT"].ToString();
                        //txtDAB50.Text = dwMU["ABPLIEFDAT"].ToString();
                        break;
                }
            }

        }
        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox tb = sender as TextBox;
            tb.Text = tb.Text.Trim();
            txtFQTYEND.Text = "-1";
            txtRQTYEND.Text = "-1";
            txtKQTYEND.Text = "-1";
            txtWQTYEND.Text = "-1";

            if (tb.Name.StartsWith("txtS"))
            {
                txtFQTYEND.Text = "-1";
                txtRQTYEND.Text = "-1";
                txtKQTYEND.Text = "-1";
                txtWQTYEND.Text = "-1";
            }

            if (tb.Name.StartsWith("txtF"))
            {
                txtRQTYEND.Text = "-1";
                txtKQTYEND.Text = "-1";
                txtWQTYEND.Text = "-1";
            }
            if (tb.Name.StartsWith("txtR"))
            {
                txtKQTYEND.Text = "-1";
                txtWQTYEND.Text = "-1";
            }
        }

        private void bmtype_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (dwBASTGA == null)
            {
                MessageBox.Show("请先选择一行");
                return;
            }
            try
            {
                ComboBox cb = sender as ComboBox;
                if (cb.SelectedIndex == -1)
                    return;
                dwBASTGA["BAGAUFMSL"] = (cb.SelectedItem as ComboBoxItem).Tag.ToString();
                cb.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void coneAddLength_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           
            if (dwBASTGA == null)
            {
                MessageBox.Show("请先选择一行");
                return;
            }
            try
            {
                ComboBox cb = sender as ComboBox;
                if (cb.SelectedIndex == -1)
                    return; 
                foreach (DataRow dwJS in SetDetail.Tables["BASTGA"].Rows)
                {
                    dwJS["ADDLENGTH"] = (cb.SelectedItem as ComboBoxItem).Tag.ToString();
                     
                }
                cb.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void btnUpdateRemark_Click(object sender, RoutedEventArgs e)
        {
            foreach (DataRow dwMU in SetDetail.Tables["ICC_ABAX"].Rows)
            {
                string ABPWAKZ = dwMU["ABPWAKZ"].ToString();
                string ABAWAKZ = dwMU["ABAWAKZ"].ToString();

                string ABAVKAUFTYP = dwMU["ABAVKAUFTYP"].ToString();
                string ABAVKAUFKRS = dwMU["ABAVKAUFKRS"].ToString();
                string ABAVKAUFPOS = dwMU["ABAVKAUFPOS"].ToString();
                string UNDES = dwMU["UNDES"].ToString();

                if (ABAVKAUFTYP == "K")
                {

                    //continue;
                }
                switch (ABAWAKZ)
                {

                    case "R":
                        dwMU["REMARK"]=REMARK ;
                        INTEXDBC.UpdateICC_ABAX(dwMU, 1);
                        break;
                }
            }
        } 
        private void REMARK_KeyUp(object sender, KeyEventArgs e)
        {
            return;
            TextBox txt = sender as TextBox;
            if(e.Key==System.Windows.Input.Key.Space)  
            {
                REMARK = txt.Text.Trim().Replace(" " ,Environment.NewLine);
           } 
        }

        private void txtDAB05_KeyUp(object sender, KeyEventArgs e)
        {

        } 
    }
}
